//
//  planningView.m
//  demo1
//
//  Created by richard_local on 23/07/13.
//  Copyright (c) 2013 ierdre.com. All rights reserved.
//

#import "planningView.h"
#import "CKCalendarView.h"
@interface planningView ()<CKCalendarDelegate>
@property(nonatomic, weak) CKCalendarView *calendar;
@property(nonatomic, strong) UILabel *dateLabel;
@property(nonatomic, strong) NSDateFormatter *dateFormatter;
@property(nonatomic, strong) NSDate *minimumDate;
@property(nonatomic, strong) NSArray *disabledDates;
@end

@implementation planningView 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    

    CKCalendarView *calendar = [[CKCalendarView alloc] initWithStartDay:startMonday];
    self.calendar = calendar;
    calendar.delegate = self;
    //calendar setmasterController = self;
    [calendar setMyMasterController:self];
    self.dateFormatter = [[NSDateFormatter alloc] init];
    [self.dateFormatter setDateFormat:@"dd/MM/yyyy"];
    self.minimumDate = [self.dateFormatter dateFromString:@"20/09/2012"];
    
    /*self.disabledDates = @[
                           [self.dateFormatter dateFromString:@"05/01/2013"],
                           [self.dateFormatter dateFromString:@"06/01/2013"],
                           [self.dateFormatter dateFromString:@"07/01/2013"]
                           ];
    */
    calendar.onlyShowCurrentMonth = NO;
    calendar.adaptHeightToNumberOfWeeksInMonth = NO;
    [self.planningView setBackgroundColor:[UIColor blackColor]];
    calendar.frame = CGRectMake(10, 0, 705, 920);
    [calendar setMySize :  CGRectMake(0, 0, 705, 920) ];
     //  calendar.frame = CGRectMake(0, 0, self.view.frame.size.height-140, self.calendar.frame.size.width-200);
    //[calendar setMySize :  CGRectMake(0, 0, self.view.frame.size.height-140, self.calendar.frame.size.width-200) ];
    [self.planningView addSubview:calendar];
   
   // calendar.delegate = self;
	// Do any additional setup after loading the view.
    
    // OK to found detail from master and other
    
  NSArray * viewControllers = self.splitViewController.viewControllers;
//   UIViewController *rootViewController = [viewControllers objectAtIndex:viewControllers.count - 2];
   NSLog (@" %@ ****** %@",[viewControllers[0] viewControllers], viewControllers);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)dateIsDisabled:(NSDate *)date {
    for (NSDate *disabledDate in self.disabledDates) {
        if ([disabledDate isEqualToDate:date]) {
            return YES;
        }
    }
    return NO;
}
#pragma mark -
#pragma mark - CKCalendarDelegate

- (void)calendar:(CKCalendarView *)calendar configureDateItem:(CKDateItem *)dateItem forDate:(NSDate *)date {
    // TODO: play with the coloring if we want to...
    if ([self dateIsDisabled:date]) {
        dateItem.backgroundColor = [UIColor redColor];
        dateItem.textColor = [UIColor whiteColor];
    }
}

- (BOOL)calendar:(CKCalendarView *)calendar willSelectDate:(NSDate *)date {
    return ![self dateIsDisabled:date];
    //return true;
}

- (void)calendar:(CKCalendarView *)calendar didSelectDate:(NSDate *)date {
    self.dateLabel.text = [self.dateFormatter stringFromDate:date];
}

- (BOOL)calendar:(CKCalendarView *)calendar willChangeToMonth:(NSDate *)date {
    if ([date laterDate:self.minimumDate] == date) {
        self.calendar.backgroundColor = [UIColor blueColor];
        return YES;
    } else {
        self.calendar.backgroundColor = [UIColor redColor];
        return NO;
    }
}

- (void)calendar:(CKCalendarView *)calendar didLayoutInRect:(CGRect)frame {
   // NSLog(@"calendar layout: %@", NSStringFromCGRect(frame));
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // make sure it's the right segue if you have more than one in this VC
    myPopover = [(UIStoryboardPopoverSegue *)segue popoverController];
}

- (IBAction)showPopover:(id)sender {
    //NSLog(@" OK POPOVER 22");
    UIButton * b = (UIButton *)sender;
    if (myPopover)
        [myPopover dismissPopoverAnimated:YES];
    else{
     //   [self performSegueWithIdentifier:@"showPop" sender:sender];
      
        
    }
}

@end
